/**
 * Created by setting on 2018/9/30.
 */
import axios from 'axios';
import {Message} from 'element-ui'
import router from '../router/index'
import store from '../store'

axios.default.withCredentials = true;
axios.interceptors.request.use(
    config => {

        config.url = `/public/index.php` + config.url;
        return config;
    },
    err => {
        return Promise.reject(err);
    });

// http response 拦截器
axios.interceptors.response.use(
    response => {
        let data = response.data;
        if (data.code !== '200') {
            Message.error(data.msg);
        }
        if (data.code === `501`) {
            store.commit('SET_USER', null)
            localStorage.removeItem('user');
            router.push('/login');

        }


        return data;
    },
    error => {
        //Toast('服务器发生错误啦');
        return Promise.reject(error.response.data)   // 返回接口返回的错误信息
    });

export default axios
